package chixing.day13.work;

import java.util.Objects;

public class Product implements Comparable<Product> {
    private String name;
    private double price;
    private int proId;
    public Product(String name, double price, int proId) {
        this.name = name;
        this.price = price;
        this.proId= proId;
    }
    public Product() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Product product = (Product) o;
        return Objects.equals(price, product.price) && Objects.equals(name, product.name);
    }

    // 重写hashCode方法，基于id和name生成哈希码
    @Override
    public int hashCode() {
        return Objects.hash(price, name);
    }
    @Override
    public String toString() {
        return "Product{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
    // Product ： this  当前商品对象
    // Product ： anotherObject 另一个商品对象
    // 定义两个商品的先后顺序的比较规则： 先比较价格price，再比较名称name，比较编号id
    // compareTo : this < anotherObject 负整数， this > anotherObject 正整数 ，this = anotherObject  0
    @Override
    public int compareTo(Product anotherPro) {
        if(this.price != anotherPro.price)
            return (int)(this.price - anotherPro.price);

        // price相同，比较name
        if(this.name .compareTo( anotherPro.name) !=0)
            return this.name .compareTo( anotherPro.name);

        // price相同， name相同，则比较编号id
        return this.proId - anotherPro.proId;

    }
}
